﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;
using System.Web.Security;
using JohnnySins.DAL.Repositories;
using System.Text;
using JohnnySins.Entities;

namespace JohnnySins
{
    public partial class JoinPost : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                NameValueCollection data = Request.Form;
                string ans = !String.IsNullOrEmpty(data["ans"]) ? data["ans"] : String.Empty;
                string country = !String.IsNullOrEmpty(data["country"]) ? data["country"] : String.Empty;
                string name = !String.IsNullOrEmpty(data["name"]) ? data["name"] : String.Empty;
                string username = !String.IsNullOrEmpty(data["username"]) ? data["username"] : String.Empty;
                string password = !String.IsNullOrEmpty(data["password"]) ? data["password"] : String.Empty;
                string email = !String.IsNullOrEmpty(data["email"]) ? data["email"] : String.Empty;
                string pi_code = !String.IsNullOrEmpty(data["pi_code"]) ? data["pi_code"] : String.Empty;
                string zip = !String.IsNullOrEmpty(data["zip"]) ? data["zip"] : String.Empty;

                try
                {
                    //Save Log user registration 

                    // form data to string
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < data.Count; i++)
                    {
                        string curItemStr = string.Format("{0}={1}", data.Keys[i], data[data.Keys[i]]);
                        if (i != 0)
                            sb.Append("&");
                        sb.Append(curItemStr);
                    }

                    string eventMessageLog = ans;
                    string stackTraceLog = Environment.StackTrace;
                    string currentUserLog = email;
                    string pathLog = Context.Request.Path.Replace("'", "''");
                    string queryStringLog = sb.ToString();
                    string browserLog = Context.Request.Browser.Browser;
                    string userHostAddressLog = Context.Request.UserHostAddress;
                    string urlReferrerLog = (Context.Request.UrlReferrer != null ? Context.Request.UrlReferrer.ToString() : "");

                    using (var repos = RepositoryFactory.ResolveRepository<LogRepository>())
                    {
                        repos.RegisterUserLog(eventMessageLog, stackTraceLog, currentUserLog, pathLog, queryStringLog, browserLog,
                            userHostAddressLog, urlReferrerLog);
                    }
                }
                catch { }


                bool needReg = true;

                // if no data
                if (string.IsNullOrEmpty(ans) || string.IsNullOrEmpty(name) || string.IsNullOrEmpty(username) || string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password))
                    needReg = false;

                //If User Exist
                if (needReg)
                {
                    string userName = Membership.GetUserNameByEmail(email);
                    if (!String.IsNullOrEmpty(userName))
                        needReg = false;
                }

                // TODO: check status  ans=[STATUS][APPROVE/DENY CODE]  STATUS: a one letter code with value Y (transaction approved) or N (denied). &ans=YGOODTEST%7c1629972669";
                if (needReg)
                    if (ans.IndexOf("Y") != 0)
                        needReg = false;

                if (needReg)  
                {
                    List<Country> countryList;
                    using (var repo = RepositoryFactory.ResolveRepository<UserRepository>())
                    {
                        countryList = repo.CountryCombo(true, false).ToList();
                    }
                    var queryCountry = country;
                    var countryCurr = countryList.FirstOrDefault(x => x.CountryName == queryCountry);

                    using (var repo = RepositoryFactory.ResolveRepository<UserRepository>())
                    {
                        User usr = new User();
                        usr.LastName = "";
                        usr.FirstName = name;
                        usr.Email = email;
                        usr.UserName = username;
                        usr.Password = password;
                        usr.Zip = zip;
                        usr.CountryId = countryList.Count > 0 && countryCurr != null ? countryCurr.CountryId : -1;

                        if (pi_code == "ciirjj1p467145") //1 month  
                        {
                            usr.PaymentTypeId = 2;
                        }
                        else if (pi_code == "ciirjj2p467146") //3 month  
                        {
                            usr.PaymentTypeId = 3;
                        }
                        else if (pi_code == "ciirjj3p531975") //6 month  
                        {
                            usr.PaymentTypeId = 4;
                        }
                        else
                        {
                            usr.PaymentTypeId = 1; //trial registration 
                        }

                        repo.AddManual(usr, usr.Password);
                    }
                }                
            }
            catch (Exception ex)
            {
            }
        }
    }
}